﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//串口通信
namespace WindowsFormsApplication1
{

    class SerialPortUtil
    {   
        //定义打开串口的list
        private List<byte[]> serialOpenList;
        //定义关闭串口的list
        private List<byte[]> serialCloseList;
        //定义查询串口的list
        private List<byte[]> serialQueryList;
        //定义一个串口
        private SerialPort serialPort; 

        public SerialPortUtil() {
            serialOpenList = new List<byte[]>();
            serialCloseList = new List<byte[]>();
            serialQueryList = new List<byte[]>();
            //打开
            byte[] serial1 = new byte[] { 0x55, 0x01, 0x12, 0x00, 0x00, 0x00, 0x01, 0x69 };
            byte[] serial2 = new byte[] { 0x55, 0x01, 0x12, 0x00, 0x00, 0x00, 0x02, 0x6a };
            byte[] serial3 = new byte[] { 0x55, 0x01, 0x12, 0x00, 0x00, 0x00, 0x03, 0x6b };
            byte[] serial4 = new byte[] { 0x55, 0x01, 0x12, 0x00, 0x00, 0x00, 0x04, 0x6c };
            byte[] serial5 = new byte[] { 0x55, 0x01, 0x12, 0x00, 0x00, 0x00, 0x05, 0x6d };
            byte[] serial6 = new byte[] { 0x55, 0x01, 0x12, 0x00, 0x00, 0x00, 0x06, 0x6e };
            byte[] serial7 = new byte[] { 0x55, 0x01, 0x12, 0x00, 0x00, 0x00, 0x07, 0x6f };
            byte[] serial8 = new byte[] { 0x55, 0x01, 0x12, 0x00, 0x00, 0x00, 0x08, 0x70 };
            byte[] serial9 = new byte[] { 0x55, 0x01, 0x12, 0x00, 0x00, 0x00, 0x09, 0x71 };
            byte[] serial10 = new byte[] { 0x55, 0x01, 0x12, 0x00, 0x00, 0x00, 0x0a, 0x72 };
            byte[] serial11 = new byte[] { 0x55, 0x01, 0x12, 0x00, 0x00, 0x00, 0x0b, 0x73 };
            byte[] serial12 = new byte[] { 0x55, 0x01, 0x12, 0x00, 0x00, 0x00, 0x0c, 0x74 };
            byte[] serial13 = new byte[] { 0x55, 0x01, 0x12, 0x00, 0x00, 0x00, 0x0d, 0x75 };
            byte[] serial14 = new byte[] { 0x55, 0x01, 0x12, 0x00, 0x00, 0x00, 0x0e, 0x76 };
            byte[] serial15 = new byte[] { 0x55, 0x01, 0x12, 0x00, 0x00, 0x00, 0x0f, 0x77 };
            byte[] serial16 = new byte[] { 0x55, 0x01, 0x12, 0x00, 0x00, 0x00, 0x10, 0x78 };
            byte[] serialAll = new byte[] { 0x55, 0x01, 0x13, 0x00, 0x00, 0xff, 0xff, 0x67 };
            serialOpenList.Add(serial1);
            serialOpenList.Add(serial2);
            serialOpenList.Add(serial3);
            serialOpenList.Add(serial4);
            serialOpenList.Add(serial5);
            serialOpenList.Add(serial6);
            serialOpenList.Add(serial7);
            serialOpenList.Add(serial8);
            serialOpenList.Add(serial9);
            serialOpenList.Add(serial10);
            serialOpenList.Add(serial11);
            serialOpenList.Add(serial12);
            serialOpenList.Add(serial13);
            serialOpenList.Add(serial14);
            serialOpenList.Add(serial15);
            serialOpenList.Add(serial16);
            serialOpenList.Add(serialAll);
            //关闭
            byte[] serialClose1 = new byte[] { 0x55, 0x01, 0x11, 0x00, 0x00, 0x00, 0x01, 0x68 };
            byte[] serialClose2 = new byte[] { 0x55, 0x01, 0x11, 0x00, 0x00, 0x00, 0x02, 0x69 };
            byte[] serialClose3 = new byte[] { 0x55, 0x01, 0x11, 0x00, 0x00, 0x00, 0x03, 0x6a };
            byte[] serialClose4 = new byte[] { 0x55, 0x01, 0x11, 0x00, 0x00, 0x00, 0x04, 0x6b };
            byte[] serialClose5 = new byte[] { 0x55, 0x01, 0x11, 0x00, 0x00, 0x00, 0x05, 0x6c };
            byte[] serialClose6 = new byte[] { 0x55, 0x01, 0x11, 0x00, 0x00, 0x00, 0x06, 0x6d };
            byte[] serialClose7 = new byte[] { 0x55, 0x01, 0x11, 0x00, 0x00, 0x00, 0x07, 0x6e };
            byte[] serialClose8 = new byte[] { 0x55, 0x01, 0x11, 0x00, 0x00, 0x00, 0x08, 0x6f };
            byte[] serialClose9 = new byte[] { 0x55, 0x01, 0x11, 0x00, 0x00, 0x00, 0x09, 0x70 };
            byte[] serialClose10 = new byte[] { 0x55, 0x01, 0x11, 0x00, 0x00, 0x00, 0x0a, 0x71 };
            byte[] serialClose11 = new byte[] { 0x55, 0x01, 0x11, 0x00, 0x00, 0x00, 0x0b, 0x72 };
            byte[] serialClose12 = new byte[] { 0x55, 0x01, 0x11, 0x00, 0x00, 0x00, 0x0c, 0x73 };
            byte[] serialClose13 = new byte[] { 0x55, 0x01, 0x11, 0x00, 0x00, 0x00, 0x0d, 0x74 };
            byte[] serialClose14 = new byte[] { 0x55, 0x01, 0x11, 0x00, 0x00, 0x00, 0x0e, 0x75 };
            byte[] serialClose15 = new byte[] { 0x55, 0x01, 0x11, 0x00, 0x00, 0x00, 0x0f, 0x76 };
            byte[] serialClose16 = new byte[] { 0x55, 0x01, 0x11, 0x00, 0x00, 0x00, 0x10, 0x77 };
            byte[] serialCloseAll = new byte[] { 0x55, 0x01, 0x13, 0x00, 0x00, 0x00, 0x00, 0x69 };
            serialCloseList.Add(serialClose1);
            serialCloseList.Add(serialClose2);
            serialCloseList.Add(serialClose3);
            serialCloseList.Add(serialClose4);
            serialCloseList.Add(serialClose5);
            serialCloseList.Add(serialClose6);
            serialCloseList.Add(serialClose7);
            serialCloseList.Add(serialClose8);
            serialCloseList.Add(serialClose9);
            serialCloseList.Add(serialClose10);
            serialCloseList.Add(serialClose11);
            serialCloseList.Add(serialClose12);
            serialCloseList.Add(serialClose13);
            serialCloseList.Add(serialClose14);
            serialCloseList.Add(serialClose15);
            serialCloseList.Add(serialClose16);
            serialCloseList.Add(serialCloseAll);
            //查询
            byte[] serialQuery1 = new byte[] { 0x55, 0x01, 0x10, 0x00, 0x00, 0x00, 0x00, 0x66 };
            serialQueryList.Add(serialQuery1);
        }
        //获得打开串口的数组
        public byte[] getSerialOpenCode(int index) {
            return serialOpenList.ElementAt(index);
        }
        //获得关闭串口的数组
        public byte[] getSerialCloseCode(int index)
        {
            return serialCloseList.ElementAt(index);
        }
        //获得查询串口的数组
        public byte[] getSeriorQueryCode(int index) {
            return serialQueryList.ElementAt(index);
        }
        //打开一个串口
        public SerialPort getSerialPort(String comName) { 
            serialPort =  new SerialPort();
            //波特率
            serialPort.BaudRate = 9600;
            //终止位
            serialPort.StopBits = StopBits.One;
            //奇偶校验
            serialPort.Parity = Parity.None;
            //读超时
            serialPort.ReadTimeout = 2000;
            //设置端口
            serialPort.PortName = comName;
            
            return serialPort;
           
        }

        //关闭一个串口
        public void close(SerialPort serialPort) {
            if (serialPort != null && serialPort.IsOpen)
            {
                //先将继电器关闭
                byte[] b = serialCloseList.ElementAt(16);
                serialPort.Write(b,0,b.Length);
                serialPort.Close();
            }
            }
         }
}
